var mongoose = require('mongoose');

var env = process.env.NODE_ENV || 'development';
var DB_URL = "mongodb://127.0.0.1:19999/test2";
if (env == 'development') {
    DB_URL = "mongodb://127.0.0.1:27017/test3";
}

mongoose.set('debug', true)
mongoose.Promise = global.Promise


mongoose.connect(DB_URL, { useNewUrlParser: true }).then(res => {
    console.log('mongodb连接成功！');
}).catch(err => {
    console.log('mongodb连接失败', err);
})

mongoose.connection.on('connected', function () {    
    console.log('Mongoose connection open to ' + DB_URL);  
});    

/**
 * 连接异常
 */
mongoose.connection.on('error',function (err) {    
    console.log('Mongoose connection error: ' + err);  
});    
 
/**
 * 连接断开
 */
mongoose.connection.on('disconnected', function () {    
    console.log('Mongoose connection disconnected');  
});    

module.exports = mongoose;